//package com.ccb.sc.conf;
//
//import com.ccb.sc.auth.TokenModel;
//import com.ccb.sc.common.constant.CommonConstant;
//import com.ccb.sc.common.util.HttpServletRequestUtil;
//import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
//import com.baomidou.mybatisplus.core.config.GlobalConfig;
//import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
//import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.ibatis.reflection.MetaObject;
//import org.mybatis.spring.annotation.MapperScan;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.transaction.annotation.EnableTransactionManagement;
//
//import javax.servlet.http.HttpServletRequest;
//import java.time.LocalDateTime;
//
///**
// * @author ken
// * @version 1.0
// * @date 2020-09-14
// */
//@Configuration
//@EnableTransactionManagement
//@MapperScan({"com.ccb.sc.**.mapper", "com.ccb.sc.common.modules.**.mapper"})
//public class MybatisPlusConf {
//
//    @Bean
//    public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
//        return mybatisPlusProperties -> {
//            String typeEnumsPackage = mybatisPlusProperties.getTypeEnumsPackage();
//            if (StringUtils.isBlank(typeEnumsPackage)) {
//                //设置默认枚举包路径
//                mybatisPlusProperties.setTypeEnumsPackage("**.enums");
//            }
//            //设置默认逻辑删除配置
//            GlobalConfig.DbConfig dbConfig = mybatisPlusProperties.getGlobalConfig().getDbConfig();
//            dbConfig.setLogicDeleteField("deleted");
//            dbConfig.setLogicDeleteValue("1");
//            dbConfig.setLogicNotDeleteValue("0");
//        };
//    }
//
//    @Bean
//    public MybatisPlusInterceptor mybatisPlusInterceptor() {
//        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        interceptor.addInnerInterceptor(paginationInnerInterceptor);
//        // mybatis-plus乐观锁插件
//        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        return interceptor;
//    }
//
//    @Bean
//    public MetaObjectHandler metaObjectHandler() {
//        return new MetaObjectHandler() {
//            @Override
//            public void insertFill(MetaObject metaObject) {
//                String username = "0";
//                HttpServletRequest request = HttpServletRequestUtil.getRequest();
//                if (request != null) {
//                    TokenModel tokenModel = (TokenModel) request.getAttribute(CommonConstant.CURRENT_USER);
//                    if (tokenModel != null) {
//                        username = tokenModel.getId() + "";
//                    }
//                }
//                this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
//                this.strictInsertFill(metaObject, "createBy", String.class, username);
//                this.strictUpdateFill(metaObject, "modifyTime", LocalDateTime.class, LocalDateTime.now());
//                this.strictInsertFill(metaObject, "modifyBy", String.class, username);
//            }
//
//            @Override
//            public void updateFill(MetaObject metaObject) {
//                String username = "0";
//                HttpServletRequest request = HttpServletRequestUtil.getRequest();
//                if (request != null) {
//                    TokenModel tokenModel = (TokenModel) request.getAttribute(CommonConstant.CURRENT_USER);
//                    if (tokenModel != null) {
//                        username = tokenModel.getId() + "";
//                    }
//                }
//                this.strictUpdateFill(metaObject, "modifyTime", LocalDateTime.class, LocalDateTime.now());
//                this.strictInsertFill(metaObject, "modifyBy", String.class, username);
//            }
//        };
//    }
//
//}
